package com.android.hedgehog.framework.impl;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.util.Log;

import com.android.hedgehog.framework.*;

public class AndroidGraphics implements Graphics {
	AssetManager assets;
	Bitmap frameBuffer;
	Canvas canvas;
	Typeface font2, font3, font4;
	Paint paint, paint2, paint3, paint4, paint5, paint6, paint7, iPaint, uPaint, uPaint2;
	Rect srcRect = new Rect();
	Rect dstRect = new Rect();
	RectF ovalRect = new RectF();

	public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
		this.assets = assets;
		this.frameBuffer = frameBuffer;
		this.canvas = new Canvas(frameBuffer);
		this.paint = new Paint();
		this.paint2 = new Paint();
		this.paint3 = new Paint();
		this.paint4 = new Paint();
		this.paint5 = new Paint(); // ARROW
		this.uPaint = new Paint();
		this.uPaint2 = new Paint();
		this.paint6 = new Paint();
		this.paint7 = new Paint();
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		paint.setTextAlign(Paint.Align.CENTER);
		paint2.setTextAlign(Paint.Align.CENTER);
		paint3.setTextAlign(Paint.Align.CENTER);
		paint4.setTextAlign(Paint.Align.CENTER);
		paint7.setTextAlign(Paint.Align.CENTER);
		paint7.setTypeface(Typeface.DEFAULT_BOLD);
		font2 = Typeface.createFromAsset(this.assets, "smallfont.ttf");
		font3 = Typeface.createFromAsset(this.assets, "futurex.ttf");
		font4 = Typeface.createFromAsset(this.assets, "stalker.ttf");
		paint2.setTypeface(font2);
		paint3.setTypeface(font3);
		paint4.setTypeface(font4);
		paint5.setStrokeWidth(3.0f);
	}

	public Pixmap newPixmap(String fileName, PixmapFormat format) {
		Config config = null;
		if (format == PixmapFormat.RGB565)
			config = Config.RGB_565;
		else if (format == PixmapFormat.ARGB4444)
			config = Config.ARGB_4444;
		else
			config = Config.ARGB_8888;

		Options options = new Options();
		options.inPreferredConfig = config;

		InputStream in = null;
		Bitmap bitmap = null;
		try {
			in = assets.open(fileName);
			bitmap = BitmapFactory.decodeStream(in);
			if (bitmap == null)
				throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");
		} catch (IOException e) {
			throw new RuntimeException("Couldn't load bitmap from asset '" + fileName + "'");
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}

		if (bitmap.getConfig() == Config.RGB_565)
			format = PixmapFormat.RGB565;
		else if (bitmap.getConfig() == Config.ARGB_4444)
			format = PixmapFormat.ARGB4444;
		else
			format = PixmapFormat.ARGB8888;

		return new AndroidPixmap(bitmap, format);
	}

	public void clear(int color) {
		canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff));
	}

	public void drawPixel(int x, int y, int color) {
		paint.setColor(color);
		canvas.drawPoint(x, y, paint);
	}

	public void drawText(String text, int x, int y, int textSize, int color) {
		paint.setColor(color);
		paint.setTextSize(textSize);
		canvas.drawText(text, x, y, paint);
	}

	public void drawHiddenText(String text, int x, int y, int alpha, int textSize, int color) {
		paint7.setColor(color);
		paint7.setTextSize(textSize);
		paint7.setAlpha(alpha);
		canvas.drawText(text, x, y, paint7);

	}

	public void drawSpecialText(String text, float x, float y, int textSize, int fontStyle, int color) {
		if (fontStyle == 2) {
			iPaint = paint2;
		} else if (fontStyle == 3) {
			iPaint = paint3;
		} else if (fontStyle == 4) {
			iPaint = paint4;
		} else {
			iPaint = paint;
		}
		iPaint.setAntiAlias(true);
		iPaint.setColor(color);
		iPaint.setTextSize(textSize);
		canvas.drawText(text, x, y, iPaint);

	}

	public void drawUniversalText(String text, int font, boolean center, float x, float y, int size,
			int alpha, int color) {
		if (font == 0) {
			uPaint2.setTypeface(Typeface.DEFAULT);
		}
		if (font == 1) {
			uPaint2.setTypeface(Typeface.DEFAULT_BOLD);
		}
		if (font == 2) {
			uPaint2.setTypeface(font2);
		}
		if (!center) {
			uPaint2.setTextAlign(Align.LEFT);
		} else {
			uPaint2.setTextAlign(Align.CENTER);
		}
		uPaint2.setAntiAlias(true);
		uPaint2.setTextSize(size);
		uPaint2.setAlpha(alpha);
		uPaint2.setColor(color);
		uPaint2.setStrokeWidth(1);
		canvas.drawText(text, x, y, uPaint2);

	}

	public void drawLine(float x, float y, float x2, float y2, int color) {
		paint.setColor(color);
		canvas.drawLine(x, y, x2, y2, paint);
	}

	public void drawUniversalLine(float x, float y, float x2, float y2, float strokeWidth, int color) {
		paint6.setColor(color);
		paint6.setStrokeWidth(strokeWidth);
		canvas.drawLine(x, y, x2, y2, paint6);

	}

	public void drawArrow(float x, float y, float x2, float y2, int color) {
		paint5.setColor(color);
		paint5.setPathEffect(new DashPathEffect(new float[] { 5, 10 }, 0));
		canvas.drawLine(x, y, x2, y2, paint5);
	}

	public void drawRect(int x, int y, int width, int height, int color) {
		paint.setColor(color);
		paint.setStyle(Style.FILL);
		canvas.drawRect(x, y, x + width, y + height, paint);
	}

	public void drawRect2(int x, int y, int width, int height, int color) {
		paint.setColor(color);
		paint.setStyle(Style.STROKE);
		canvas.drawRect(x, y, x + width - 1, y + height - 1, paint);
	}

	public void drawRect3(int x, int y, int width, int height, int A, int R, int G, int B) {
		paint.setARGB(A, R, G, B);
		paint.setStyle(Style.FILL);
		canvas.drawRect(x, y, x + width, y + height, paint);
	}

	public void drawUniversalRect(int x, int y, int width, int height, boolean oval, boolean fillRect,
			float strokeWidth, int alpha, int color) {
		if (fillRect) {
			uPaint.setStyle(Style.FILL);
		}
		if (!fillRect) {
			uPaint.setStyle(Style.STROKE);
		}
		uPaint.setAlpha(alpha);
		uPaint.setColor(color);
		uPaint.setStrokeWidth(strokeWidth);

		if (oval) {
			ovalRect.top = y;
			ovalRect.left = x;
			ovalRect.bottom = y + height;
			ovalRect.right = x + width;

			canvas.drawOval(ovalRect, uPaint);
		} else {
			canvas.drawRect(x, y, x + width, y + height, uPaint);
		}

	}

	public void drawGradient(int x, int y, int width, int height, int color1, int color2) {
		paint.setColor(color1);
		paint.setAntiAlias(true);
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setStyle(Style.FILL);
		ovalRect.top = y;
		ovalRect.left = x;
		ovalRect.bottom = y + height;
		ovalRect.right = x + width;

		canvas.drawCircle(this.getWidth() / 4, this.getHeight() / 4, 40, paint);

		/*
		 * canvas.drawOval(ovalRect, paint); canvas.drawC
		 * paint.setStyle(Style.STROKE); paint.setColor(color2);
		 * canvas.drawOval(ovalRect, paint);
		 */
		/*
		 * canvas.drawText("The Fox", 80, 190, paint); canvas.drawRect(50, 200,
		 * 110, 260, paint);
		 * 
		 * canvas.drawText("The Fox", 170, 190, paint); canvas.drawRect(140,
		 * 200, 200, 260, paint);
		 * 
		 * Paint paint6 = paint2; paint6.setTextSize(24);
		 * paint6.setTypeface(font2); paint6.setTextAlign(Paint.Align.CENTER);
		 * canvas.drawText("The Fox", 80, 290, paint6); canvas.drawRect(50, 300,
		 * 110, 360, paint6);
		 * 
		 * paint6.setTypeface(Typeface.SERIF);
		 * 
		 * paint2.setTextAlign(Paint.Align.LEFT); canvas.drawText("The Fox",
		 * 170, 290, paint6); canvas.drawRect(140, 300, 200, 360, paint6);
		 */

		// paint.setShader(new LinearGradient(0, 0, 0, getHeight(), color1,
		// color2, Shader.TileMode.MIRROR));
		// paint.setAlpha(x);
		// paint.setColor(color2);
		// paint.setStyle(Style.STROKE);
		// canvas.drawRect(100, 300, 180, 360, paint);
		// canvas.drawRect(200, 200, 280, 360, paint);

	}

	public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight) {
		srcRect.left = srcX;
		srcRect.top = srcY;
		srcRect.right = srcX + srcWidth;
		srcRect.bottom = srcY + srcHeight;

		dstRect.left = x;
		dstRect.top = y;
		dstRect.right = x + srcWidth;
		dstRect.bottom = y + srcHeight;

		canvas.drawBitmap(((AndroidPixmap) pixmap).bitmap, srcRect, dstRect, null);
	}

	public void drawFlippedPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight) {
		srcRect.left = srcX;
		srcRect.top = srcY;
		srcRect.right = srcX + srcWidth;
		srcRect.bottom = srcY + srcHeight;

		dstRect.left = x;
		dstRect.top = y;
		dstRect.right = x + srcWidth;
		dstRect.bottom = y + srcHeight;

		canvas.drawBitmap(((AndroidPixmap) pixmap).bitmap, srcRect, dstRect, null);
	}

	public void drawPixmap(Pixmap pixmap, int x, int y) {
		canvas.drawBitmap(((AndroidPixmap) pixmap).bitmap, x, y, null);
	}

	public void drawBackground(int color) {
		canvas.drawColor(color);
	}

	/**
	 * Use this for all backgrounds
	 */
	public void clear(boolean fullScreen, int color) {
		if (!fullScreen) {
			paint.setColor(color);
			canvas.drawRect(0, 31, 320, 480, paint);

		} else
			canvas.drawColor(color);

	}

	public int getWidth() {
		return frameBuffer.getWidth();
	}

	public int getHeight() {
		return frameBuffer.getHeight();
	}
}
